package com.xiaoma.thread.interrupt;

public class InterruptTest {

  public static void main(String[] args) {

    Object obj=new Object();
    Thread thread = new Thread(()->{
      synchronized (obj) {
        try {
          System.out.println("begin");
          obj.wait();
          System.out.println("end"); //阻塞的线程抛出异常，响应中断，不会打印“end"
        } catch (InterruptedException e) {
          System.out.println("捕获到异常，说明已经中断");
          e.printStackTrace();
        }
      }

    }) ;


    thread.start();

    try {
      Thread.sleep(1000 * 5);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    thread.interrupt();//作用是：在线程阻塞时抛出一个中断信号，这样线程就得以退出阻塞的状态
  }
}
